package model

import (
	"stock/global"
	"sync"

	"gorm.io/gorm"
)

type SupplierModel struct {
	Base
}

type MallSupplier struct {
	Id             uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	Logo           string `json:"logo" gorm:"column:logo;"`
	CountryCode    string `json:"country_code" gorm:"<-:false; column:country_code;"`
	ContactPhone   string `json:"contact_phone" gorm:"column:contact_phone;"`
	CategoryId     uint   `json:"category_id" gorm:"column:category_id;"`
	Status         int    `json:"status" gorm:"column:status;"`
	IsDelete       uint   `json:"soris_deletet" gorm:"column:is_delete;"`
	UserId         uint   `json:"user_id" gorm:"<-:false; column:user_id;"`
	ExpirationDate uint   `json:"expiration_date" gorm:"column:expiration_date;"`
	CheckState     uint8  `json:"check_state" gorm:"column:check_state;"`
	IsRecommend    uint8  `json:"is_recommend" gorm:"column:is_recommend;"`
	Email          string `json:"email" gorm:"column:email;"`
	Sort           string `json:"sort" gorm:"column:sort;"`
	Theme          string `json:"theme" gorm:"column:theme;"`
	Perfect        string `json:"perfect" gorm:"column:perfect;"`
	SupplierLang   string `json:"lang" gorm:"column:lang;"`
	CreateAt       string `json:"create_at" gorm:"<-:false; column:create_at;"`
	UpdatedAt      string `json:"updated_at" gorm:"column:updated_at;"`
}

type RequestSupplier struct {
	Id          uint
	CountryCode string
	Theme       string
	Perfect     []string
	CategoryId  uint
	UserId      uint
	Status      uint8
	CheckState  uint8
	IsDelete    uint
	IdArr       []uint
}

type ResponseSupplier struct {
	MallSupplier
	ResponseLang
	ResponseFile
	MerchantId   uint   `json:"merchant_id"`
	SupplierName string `json:"supplier_name"`
}

func (m *SupplierModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_supplier"
}

func (m *SupplierModel) ExtendParam() map[string]string {
	var param = make(map[string]string)
	param["merchant_type"] = "1"
	param["table_name"] = m.TabName()
	return param
}

// 列表
func (m *SupplierModel) List(param RequestSupplier, page global.PageSort) ([]ResponseSupplier, error) {

	where := &MallSupplier{}
	if param.CountryCode != "" {
		where.CountryCode = param.CountryCode
	}
	if param.Theme != "" {
		where.Theme = param.Theme
	}
	if param.CategoryId != 0 {
		where.CategoryId = param.CategoryId
	}
	if param.UserId != 0 {
		where.UserId = param.UserId
	}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	if param.IsDelete != 0 {
		where.IsDelete = param.IsDelete
	}
	var supplier []MallSupplier
	var tx *gorm.DB
	tx = global.DB.Debug().Where(where).Order(page.Sortfield + " " + page.Sort)
	if len(param.IdArr) > 0 && len(param.Perfect) > 0 {
		tx = tx.Where("id IN ?", param.IdArr).Where("perfect IN ?", param.Perfect)
	} else if len(param.IdArr) > 0 {
		tx = tx.Where("id IN ?", param.IdArr)
	} else if len(param.Perfect) > 0 {
		tx = tx.Where("perfect IN ?", param.Perfect)
	}
	tx.Offset(page.Page).Limit(page.Limit).Find(&supplier)
	if tx.Error != nil {
		return nil, tx.Error
	}
	supplierLen := len(supplier)
	var supplierLangArr []ResponseSupplier

	if supplierLen > 0 {
		var supplierLangModel *LangModel = new(LangModel)
		var supplierWg sync.WaitGroup
		supplierWg.Add(supplierLen)
		for i := 0; i < supplierLen; i++ {
			go m.manyTabData(m.TabName(), supplier[i], supplierLangModel, &supplierLangArr, &supplierWg)
		}
		supplierWg.Wait()
	}
	return supplierLangArr, nil
}

// 基础详情
func (m *SupplierModel) BaseDetail(param RequestSupplier) {}

// 详情
func (m *SupplierModel) Detail(param RequestSupplier) (ResponseSupplier, error) {

	where := &MallSupplier{}
	if param.Id != 0 {
		where.Id = param.Id
	}
	if param.CountryCode != "" {
		where.CountryCode = param.CountryCode
	}
	if param.Theme != "" {
		where.Theme = param.Theme
	}
	if param.CategoryId != 0 {
		where.CategoryId = param.CategoryId
	}
	if param.UserId != 0 {
		where.UserId = param.UserId
	}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	if param.IsDelete != 0 {
		where.IsDelete = param.IsDelete
	}
	var supplier MallSupplier
	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.Perfect) > 0 {
		tx = tx.Where("perfect IN ?", param.Perfect)
	}
	tx.Order("id DESC").First(&supplier)
	var supplierLangArr []ResponseSupplier
	if supplier.Id != 0 {
		var supplierLangModel *LangModel = new(LangModel)
		var supplierWg sync.WaitGroup
		supplierWg.Add(1)
		go m.manyTabData(m.TabName(), supplier, supplierLangModel, &supplierLangArr, &supplierWg)
		supplierWg.Wait()
	}
	return supplierLangArr[0], nil
}

func (m *SupplierModel) manyTabData(tabName string, sup MallSupplier, sLangM *LangModel, supplierLangArr *[]ResponseSupplier, wg *sync.WaitGroup) {
	var reLang ResponseLang
	var supLang ResponseSupplier
	reLang, _ = sLangM.List(RequestLang{
		TableName:    tabName,
		MerchantType: 1,
		MerchantId:   sup.Id,
		TableId:      sup.Id,
		Lang:         global.LANG_COUNTRY.Lang,
		Field:        []string{"description", "main_product", "address", "supplier_name"},
	})
	supLang.Id = sup.Id
	supLang.MerchantId = sup.Id
	supLang.Logo = sup.Logo
	supLang.CategoryId = sup.CategoryId
	supLang.MerchantName = reLang.MerchantName
	supLang.SupplierName = reLang.MerchantName
	if reLang.Description != "" {
		supLang.Description = reLang.Description
		supLang.Describe = reLang.Description
	}
	supLang.Contactname = reLang.Contactname
	supLang.Contactname = sup.ContactPhone
	supLang.ExpirationDate = sup.ExpirationDate
	supLang.MainProduct = reLang.MainProduct
	supLang.Address = reLang.Address
	supLang.MainProduct = reLang.MainProduct
	supLang.Contactname = reLang.Contactname

	*supplierLangArr = append(*supplierLangArr, supLang)
	wg.Done()
}
